<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'Savant2/Plugin.php';

class Savant2_Plugin_thumb extends Savant2_Plugin{

	function plugin($type, $filename, $width, $height, $attributes = ""){
		global $mosConfig_absolute_path, $mosConfig_live_site;
		
		switch($type){
			case 'players':
				$folder = 'stories/leagueman/players';
			break;
			
			case 'teams':
				$folder = 'stories/leagueman/teams';
			break;
			
			case 'logos':
				$folder = 'stories/leagueman/logos';
			break;
			
			case 'contacts':
				$folder = 'stories/leagueman/contacts';
			break;
		}
		
		$srcfilename = $mosConfig_absolute_path . "/images/$folder/" . $filename;
		
		if(!is_file($srcfilename)){
			$filename = "default.jpg";
			$srcfilename = $mosConfig_absolute_path . "/images/$folder/default.jpg";
		}
		
		$dir = "/images/$folder/thumbs_" . $width;
		$image = $dir . "/" . $filename;
		
		$output_filename = $mosConfig_absolute_path . $image ;
		if(file_exists($output_filename)){
			return '<img src="' . $mosConfig_live_site . $image . '" border="0" ' . $attributes . ' alt="" />';
		}
		
		list($prefix,$suffix) = explode(".", $filename);
	
		require_once 'phpthumb.class.php';
		
		$phpThumb = new phpThumb();
		$phpThumb->setParameter('config_output_format', $suffix);
		$phpThumb->setParameter('w', intval($width));
		if(intval($height) > 0){
			$phpThumb->setParameter('h', intval($height));
		}
		$phpThumb->setParameter('far', 1); //fixed aspect ration
		$phpThumb->setParameter('aoe', 1); // allow output enlargement
		
		if(!is_dir($mosConfig_absolute_path . $dir)){
			$this->makePath($mosConfig_absolute_path . $dir);
		}
		
		$phpThumb->setSourceFilename($srcfilename);
		
		if(!file_exists($output_filename)){
			if ($phpThumb->GenerateThumbnail()) {
				if(!$phpThumb->RenderToFile($output_filename)){
					return false;
				}
			} else {
				return false;
			}
		}
		
		return '<img src="' . $mosConfig_live_site . $image . '" border="0" ' . $attributes . ' alt="" />';
		
    }
	
	function makePath($base, $path='', $mode = NULL) {
		global $mosConfig_dirperms;
		
		$path = str_replace( '\\', '/', $path );
		$path = str_replace( '//', '/', $path );
		
		if (file_exists( $base . $path )) return true;
		
		$origmask = NULL;
		if (isset($mode)) {
			$origmask = @umask(0);
		} else {
			if ($mosConfig_dirperms=='') {
				$mode = 0777;
			} else {
				$origmask = @umask(0);
				$mode = octdec($mosConfig_dirperms);
			}
		}
		
		$parts = explode( '/', $path );
		$n = count( $parts );
		$ret = true;
		
		if ($n < 1) {
			$ret = @mkdir($base, $mode);
		} else {
			$path = $base;
			
			for ($i = 0; $i < $n; $i++) {
				$path .= $parts[$i] . '/';
				if (!file_exists( $path )) {
					if (!@mkdir(substr($path,0,-1),$mode)) {
						$ret = false;
						break;
					}
				}
			}
		}
		
		if (isset($origmask)) {
			@umask($origmask);
		}

		return $ret;
	}
}
?>